/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-05-24 09:56:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-04-15 16:21:23
 * @FilePath: \lenercom_bluetooth_app\lib\pages\Home\Controller\AppManagerController.dart
 * @Description: app管理
 */
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info/package_info.dart';
import 'package:sil_utils_package/tools/SilTools.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../api/ApiUtils.dart';
import '../../../api/request/CommonRequest.dart';
import '../../../api/request/UserRequest.dart';
import '../../../common/module/WebViewPage/WebViewPage.dart';
import '../../../config/AppInfo.dart';
import '../../../entities/UpdateEntity.dart';
import '../../../main.dart';
import '../../../utils/router/ARouter.dart';

class AppManagerController extends GetxController {
  /// 当前app版本
  RxString appVersion = '1.0.0'.obs;

  /// 最新App版本
  RxString? lastVersionName = ''.obs;

  /// 文案
  String copyWriters = '';

  /// 获取当前app版本号
  getAppVersion() {
    PackageInfo.fromPlatform().then((value) {
      this.appVersion.value = "${value.version}";
    });
  }

  /// 获取线上app最新版本
  getLastAppVersion() {
    apiShow(navigatorKey.currentState!.context, api: api_app_version(),
        success: (res) {
      UpdateEntity appLastVersionEntiy = res.data;
      this.lastVersionName?.value = appLastVersionEntiy.versionName ?? '';
    }, showLoading: false);
  }

  /// 跳转到APP下载页面
  gotoAppDownloadPage(BuildContext context) async {
    PackageInfo info = await PackageInfo.fromPlatform();

    String url = Platform.isIOS
        ? 'https://apps.apple.com/cn/app/lenercom/id1589364293'
        : "https://play.google.com/store/apps/details?id=com.lenercom.yunmell668";

    if (info.appName == '户用储能监测与管理系统') {
      // 应用宝下载地址
      url = 'https://a.app.qq.com/o/simple.jsp?pkgname=com.lenercom.yunmell668';
    }

    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url));
    }
  }

  /// 获取线上app最新版本
  lastAppVersion() {
    api_app_version();
  }

  /// 绑定推送id
  bindPushCid() {
    if (!SilTools.stringIsEmpty(AppInfo.registrationId)) {
      api_bind_push_cid(AppInfo.registrationId);
    }
  }
}
